package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.新建一个ShoppingCart对象，属性拷贝，设置用户id，
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //1.1调用ShoppingCartMapper查询数据库,判断当前对象是否已经存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if(shoppingCartList != null && shoppingCartList.size() > 0){//当前对象已经存在
            //获取集合中的第一个对象
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //更新更新商品数量
            shoppingCartMapper.updateNumberById(shoppingCart);

        }else { //当前对象不存在
            Long dishId = shoppingCartDTO.getDishId();
            //2.1判断是套餐还是菜品
            if (dishId != null){
                //添加到购物车的是菜品,获取菜品相关信息
                Dish dish = dishMapper.getDishById(dishId);
                //设置购物车中菜品信息
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else { //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //创建一个shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //属性对拷
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //1.先根据传来的商品信息查询数据库，商品数量
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //获取集合中的第一个对象
        shoppingCart = shoppingCartList.get(0);

        //2.如果商品数量大于1，则做修改，数量减一
        if (shoppingCart.getNumber() > 1){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            //更新商品数量
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //3.商品数量等于1，则删除
            shoppingCartMapper.sub(shoppingCart);
        }
    }
}
